package com.gitee.lw114514.webapp_memorandium.controller;

import com.gitee.lw114514.webapp_memorandium.service.CompleteService.CompleteService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin(origins = {"http://localhost:5500", "http://127.0.0.1:5500"}, allowCredentials = "true", allowedHeaders = "*", methods = {RequestMethod.GET, RequestMethod.POST, RequestMethod.OPTIONS})
public class CompleteTaskController {
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class CompleteTaskRequest {
        private Integer task_id;
    }

    //1.引入service层
    @Autowired
    private CompleteService completeService;

    //2.处理请求
    @RequestMapping(value = "/api/complete_task", method = RequestMethod.PATCH)
    public Boolean completeTask(@RequestBody CompleteTaskRequest request) {
        Integer taskId = request.getTask_id();

        System.out.println("(controller)完成任务：" + taskId);
        //3.调用service层，获取数据
        Boolean result = completeService.completeTask(taskId);
        //4.返回添加结果（成功、失败）
        return result;
    }


    @RequestMapping(value = "/api/complete_task/debug", method = RequestMethod.PATCH)
    public String debugCompleteTaskData(@RequestBody CompleteTaskRequest request) {
        Integer taskId = request.getTask_id();
        System.out.println("=== 原始请求数据拦截 ===");
        System.out.println("(controller)完成任务：" + taskId);
        return "success";
    }
}
